'''
Created on May 31, 2010

@author: Guan Gui, Will Zhang
$LastChangedBy: guiguan $
'''
from collections import defaultdict

__version__ = "$Rev: 359 $"

class TranslationModel(defaultdict):
    """
    This class generates a Translation Model from C{AlignedSentCollection} for 
    Phrase Based Translation Systems. 
    """
   
    def __init__(self, aligned_sent_collection):
        '''
        Initialize a new C{TranslationModel} using those word-alignment
        information
        
        @param aligned_sent_collection: collection of all C{AlignedSent} in a 
        given corpus
        @type aligned_sent_collection: C{AlignedSentCollection}
        '''
        super(TranslationModel, self).__init__(FreqDict)
        for aligned_sent in aligned_sent_collection.sents:
            alignment = aligned_sent.alignment.invert()
            for i, f in enumerate(aligned_sent.mots):
                for e in [aligned_sent.words[i2] for _, i2 in alignment[i]]:
                    self[f][e] += 1
        for v in self.values():
            total = 0.0
            for v1 in v.values():
                total += v1
            for k in v:
                v[k] = v[k] / total
 
class FreqDict(defaultdict):
                
    def __init__(self):
        super(FreqDict, self).__init__(float)